package com.gxa.community.admin.service.bill.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxa.community.admin.mapper.bill.BillCommissionMapper;
import com.gxa.community.admin.service.bill.BillCommissionService;
import com.gxa.community.core.pojo.domain.BillCommission;
import com.gxa.community.core.pojo.dto.bill.BillCommissionDto;
import com.gxa.community.core.pojo.dto.bill.storage.BillCommissionStorageDto;
import com.gxa.community.core.pojo.vo.admin.bill.BillCommissionVo;
import com.gxa.community.core.pojo.vo.admin.bill.expand.BillCommissionVoE;
import com.gxa.community.core.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author juran
 * @since 2022-07-19
 */
@Service
public class BillCommissionServiceImpl extends ServiceImpl<BillCommissionMapper, BillCommission> implements BillCommissionService {

    @Autowired
    BillCommissionMapper billCommissionMapper;

    /**
     * 查询过团长佣金明细
     *
     * @param billCommissionDto
     * @return
     */
    @Override
    public R queryUserCommissionInfo(BillCommissionDto billCommissionDto,
                                     long page,
                                     long size) {
        Page<BillCommission> nr = new Page<>(page,size);
        //构造条件
        QueryWrapper<BillCommission> wrapper = new QueryWrapper<>();

        if (billCommissionDto.getOrderNo() != null && billCommissionDto.getOrderNo() != ' ')
            wrapper.eq("order_no", billCommissionDto.getOrderNo());

        //根据团长查询订单
        /*if (billCommissionDto.() != null && billCommissionDto.getOrderNo() != ' ')
            wrapper.eq("order_no",billCommissionDto.getOrderNo());*/

        //如果有起始时间那么，结束时间为当前，只有结束时间，必须有起始时间。
        if (billCommissionDto.getStartTime() != null && billCommissionDto.getEndTime() != null)
            wrapper.between("time", billCommissionDto.getStartTime(), billCommissionDto.getEndTime());


        if (billCommissionDto.getStartTime() != null)
            wrapper.between("time", billCommissionDto.getStartTime(), System.currentTimeMillis());

        if (billCommissionDto.getStatus() != null)
            wrapper.eq("status", billCommissionDto.getStartTime());

        Page<BillCommission> selectPage = billCommissionMapper.selectPage(nr, wrapper);

        //查询到账佣金和未到账佣金信息
        QueryWrapper<BillCommission> commissionCount = new QueryWrapper<>();

        commissionCount
                .select("IFNULL(sum(actual_amount),0) as actual_amount")
                .eq("status", 1);
        Map<String, Object> map = getMap(commissionCount);

        commissionCount
                .select("IFNULL(sum(actual_amount),0) as actual_amount")
                .eq("status", 2);
        Map<String, Object> map2 = getMap(commissionCount);

        //vo进行数据返回
        BillCommissionVo billCommissionVo = new BillCommissionVo();
        billCommissionVo.setBillCommissionVo(
                selectPage.getRecords().stream().map(
                        p -> {
                            return new BillCommissionVoE()
                                    .setOrderTime(p.getOrderTime())
                                    .setOrderNo(p.getOrderNo())
                                    .setEstimateAmount(p.getEstimateAmount())
                                    .setActualAmount(p.getActualAmount())
                                    .setStatus(p.getStatus())
                                    .setTime(p.getTime());
                        }
                ).collect(Collectors.toList())
        );
        //未到账和已到账
        billCommissionVo.setAlreadyAmountStatistics((BigDecimal) map.get("actual_amount"));
        billCommissionVo.setExceptAmountStatistics((BigDecimal) map2.get("actual_amount"));

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("total", selectPage.getTotal());
        hashMap.put("pages", selectPage.getPages());
        hashMap.put("pageSize", selectPage.getSize());
        hashMap.put("currentPage", selectPage.getCurrent());
        hashMap.put("list", billCommissionVo);

        return R.ok().put("data", hashMap);
    }

    /**
     * 添加佣金记录
     * @param billCommissionStorageDto
     * @return
     */
    @Override
    public R addCommissionInfo(BillCommissionStorageDto billCommissionStorageDto) {

        boolean save = save(new BillCommission()
                .setOrderTime(billCommissionStorageDto.getOrderTime())
                .setAgentId(billCommissionStorageDto.getAgentId())
                .setEstimateAmount(billCommissionStorageDto.getEstimateAmount())
                .setActualAmount(billCommissionStorageDto.getActualAmount())
                .setOrderNo(billCommissionStorageDto.getOrderNo())
                .setStatus(billCommissionStorageDto.getStatus()));

        return save ? R.ok("添加成功") : R.error("添加失败");
    }
}
